<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['statuss']=$this->chuku_status;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['paytype']['offpay']='线下收款';
$return['paytype']['offsao']='线下扫码';
$return['ispay']=array('0'=>'未支付_No pay','1'=>'已支付_Is pay');
if($_RQ['op']=='list' || $_RQ['op']=='diaolist' || $_RQ['op']=='panlist'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if($_RQ['op']=='list'){
		$params['type']=0;
		$tabs=array(
			array('op'=>'list','title'=>'要货单'),
			array('op'=>'edit0','title'=>'新增要货单'),
		);
	}elseif($_RQ['op']=='diaolist'){
		$params['type']=1;
		$tabs=array(
			array('op'=>'diaolist','title'=>'调拨单'),
			array('op'=>'edit1','title'=>'新增调拨单'),
		);
	}elseif($_RQ['op']=='panlist'){
		$params['type']=2;
		$tabs=array(
			array('op'=>'panlist','title'=>'盘点单'),
			array('op'=>'edit2','title'=>'新增盘点单'),
		);
	}
	$_SESSION['typeop']=$_RQ['op'];
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=$_RQ['ordersn'];
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_erp_kuorder',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']),array('title','image','unit'));
			$item['title']=$goodsinfo['title'];
			$item['image']=$goodsinfo['image'];
			
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'total','title'=>'数量','type'=>'string'),
			array('name'=>'unit','title'=>'单位','type'=>'string'),
			array('name'=>'price','title'=>'零售价','type'=>'string'),
			array('name'=>'total_price','title'=>'零售金额','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'status','width'=>10,'title'=>'订单状态','type'=>'radio','options'=>'status'),
		)
	);
	if($_RQ['op']=='diaolist'){
		$htmls['list']['list'][]=array('name'=>'diaotype','title'=>'调拨类型','type'=>'radio','options'=>'diaotype');
		$htmls['list']['list'][]=array('name'=>'storeid1','title'=>'出库门店','type'=>'select','options'=>'stores','show'=>array('diaotype'=>array(1,2)));
		$htmls['list']['list'][]=array('name'=>'storeid2','title'=>'入库门店','type'=>'select','options'=>'stores','show'=>array('diaotype'=>array(1,2)));
	}
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['diaotype']=array('平台调拨给门店','门店调拨给门店','门店调拨给平台');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit0' || $_RQ['op']=='edit1' || $_RQ['op']=='edit2'){
	if($_SESSION['typeop']=='list'){
		$type=0;
		$tabs=array(
			array('op'=>'list','title'=>'要货单'),
			array('op'=>'edit0','title'=>'新增要货单'),
		);
	}elseif($_SESSION['typeop']=='diaolist'){
		$type=1;
		$tabs=array(
			array('op'=>'diaolist','title'=>'调拨单'),
			array('op'=>'edit1','title'=>'新增调拨单'),
		);
	}elseif($_SESSION['typeop']=='panlist'){
		$type=2;
		$tabs=array(
			array('op'=>'panlist','title'=>'盘点单'),
			array('op'=>'edit2','title'=>'新增盘点单'),
		);
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!intval($post['goodsid'])){
			exi('请选择商品！','error');
		}
		if(intval($post['total'])<=0){
			exi('请填写数量，必须是大于0的整数！','error');
		}
		if(!$post['diaotype'] && !$post['storeid2']){
			exi('请选择入库门店','error');
		}
		if($post['diaotype']==1 && !$post['storeid1']){
			exi('请选择出库门店','error');
		}
		if($post['diaotype']==1 && !$post['storeid2']){
			exi('请选择入库门店','error');
		}
		if($post['diaotype']==1 && $post['storeid1']==$post['storeid2']){
			exi('出库门店不能跟入库门店一样！','error');
		}
		if($post['diaotype']==2 && !$post['storeid1']){
			exi('请选择出库门店','error');
		}
		$data=array(
			'groupid'=>trim($post['groupid']),
			'group2id'=>trim($post['group2id']),
			'unit'=>trim($post['unit']),
			'total'=>trim($post['total']),
			'price'=>trim($post['price']),
			'goodsid'=>trim($post['goodsid']),
			'remark'=>trim($post['remark']),
			'diaotype'=>trim($post['diaotype']),
			'storeid1'=>trim($post['storeid1']),
			'storeid2'=>trim($post['storeid2']),
		);
		if(!$id){
			if(!$data['diaotype']){
				$total=pdo_getcolumn('jy_weishop_goods',array('id'=>$data['goodsid'],'uniacid'=>$_SESSION['uniacid']),'total');
				if($total<$data['total']){
					exi('该商品库存不足！仅剩'.$total,'error');
				}
				pdo_update('jy_weishop_goods',array('total -='=>$data['total']),array('id'=>$data['goodsid'],'uniacid'=>$_SESSION['uniacid']));
				pdo_update('jy_weishop_store_goods',array('total +='=>$data['total']),array('goodsid'=>$data['goodsid'],'storeid'=>$data['storeid2'],'uniacid'=>$_SESSION['uniacid']));
			}elseif($data['diaotype']==1){
				$total=$this->model('goods')->goodstotal($data['goodsid'],$data['storeid1']);
				if($total<$data['total']){
					exi('该商品库存不足！仅剩'.$total,'error');
				}
				pdo_update('jy_weishop_store_goods',array('total -='=>$data['total']),array('goodsid'=>$data['goodsid'],'storeid'=>$data['storeid1'],'uniacid'=>$_SESSION['uniacid']));
				pdo_update('jy_weishop_store_goods',array('total +='=>$data['total']),array('goodsid'=>$data['goodsid'],'storeid'=>$data['storeid2'],'uniacid'=>$_SESSION['uniacid']));
			}elseif($data['diaotype']==2){
				$total=$this->model('goods')->goodstotal($data['goodsid'],$data['storeid1']);
				if($total<$data['total']){
					exi('该商品库存不足！仅剩'.$total,'error');
				}
				pdo_update('jy_weishop_store_goods',array('total -='=>$data['total']),array('goodsid'=>$data['goodsid'],'storeid'=>$data['storeid1'],'uniacid'=>$_SESSION['uniacid']));
				pdo_update('jy_weishop_goods',array('total +='=>$data['total']),array('id'=>$data['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			}
			$data['uniacid']=$_SESSION['uniacid'];
			$data['type']=$type;
			$data['createtime']=time();
			pdo_insert('jy_erp_kuorder',$data);
		}else{
			pdo_update('jy_erp_kuorder',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('操作成功！','',$_SESSION['typeop']);
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_erp_kuorder',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$return['item']=$item?:array('type'=>$type);
	$htmls=array(
		'type'=>'edit',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'select2_line','name'=>'groupid','options'=>'groups','title'=>'选择部门',
				'name2'=>'group2id','options2'=>'groups2','title2'=>'选择职位',
			),
			array('name'=>'diaotype','title'=>'调拨类型','type'=>'radio','options'=>'diaotype'),
			array('name'=>'storeid1','title'=>'出库门店','type'=>'select','options'=>'stores','show'=>array('diaotype'=>array(1,2))),
			array('name'=>'storeid2','title'=>'入库门店','type'=>'select','options'=>'stores','show'=>array('diaotype'=>array(0,1))),
			array('type'=>'choose','title'=>'选择商品','name'=>'goodsid','action'=>'jy_weishop/goods.get_goods/','choose_title'=>'选择商品'),
			array('name'=>'total','title'=>'数量','type'=>'string'),
			array('name'=>'unit','title'=>'单位','type'=>'string'),
			array('name'=>'price','title'=>'零售价','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'text'),
		)
	);
	$user_juese=$this->user_juese;
	$return['groups']=array();
	foreach($user_juese as $key=>$title){
		$return['groups'][$key]=array('id'=>$key,'title'=>$title);
		$pv['groups2']=pdo_getall('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'juese'=>$key),array('title','id'),'id');
	}
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['diaotype']=array('平台调拨给门店','门店调拨给门店','门店调拨给平台');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_erp_kuorder',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
